/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.businesslogic;

import com.bionic.ce.dao.IUserDAO;
import com.bionic.ce.beans.User;
import com.bionic.ce.connections.DAOFactory;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Jarik
 */
public class UserService {

    public static IUserDAO userDAO;

    static {
        DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
        userDAO = factory.getUserDAO();
    }

    public static Integer createNewUser(String name, String surName, String email,
            String password, String adress, String telephone) throws IllegalArgumentException, IllegalStateException {
        Integer result;
        User user = new User();
        if (name.isEmpty() | surName.isEmpty() | email.isEmpty() | password.isEmpty() | telephone.isEmpty()) {
            throw new IllegalStateException("Not all required fields are filled");
        } else {
            if (checkUserLoginAndPassword(email, null) != null) {
                throw new IllegalArgumentException("Such email is already registered");
            } else {
                user.setName(name);
                user.setSurName(surName);
                user.setEmail(email);
                user.setPassword(password);
                user.setAdress(adress);
                user.setTelephone(telephone);
                try {
                    userDAO.add(user);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                result = checkUserLoginAndPassword(email, password);
            }
        }
        return result;
    }

    public static void updateCurrentUser(Integer userId, String name, String surName, String email,
            String password, String adress, String telephone) {
        User user = new User();
        if (name.isEmpty() | surName.isEmpty() | email.isEmpty() | password.isEmpty() | telephone.isEmpty()) {
            throw new IllegalStateException("Updates was not effected because not all required fields were filled");
        } else {
            user.setUserid(userId);
            user.setName(name);
            user.setSurName(surName);
            user.setEmail(email);
            user.setPassword(password);
            user.setAdress(adress);
            user.setTelephone(telephone);
            try {
                userDAO.update(user);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void deleteCurrentUser(Integer userId) {
        try {
            userDAO.deleteById(userId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static User returnCurrentUserInfo(Integer userId) {
        User user = new User();
        user.setUserid(userId);
        try {
            List<User> list = userDAO.findByDynamicSelect(user);
            user = ((User) list.toArray()[0]);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return user;
    }

    public static Integer checkUserLoginAndPassword(String email, String password) {
        Integer userId = null;
        User user = new User();
        if (email != null) {
            user.setEmail(email);
        }
        if (password != null) {
            user.setPassword(password);
        }
        user.setStatus(null);
        try {
            List<User> list = userDAO.findByDynamicSelect(user);
            if (!list.isEmpty()) {
                userId = ((User) list.toArray()[0]).getUserid();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return userId;
    }
}
